package com.androidvizlab.bachelor.customcomponents.listenersandadapters;

import java.util.ArrayList;

import com.androidvizlab.bachelor.R;
import com.androidvizlab.bachelor.customcomponents.viewholder.CamComboListViewHolder;
import com.androidvizlab.bachelor.customcomponents.viewholder.CamInfoListViewHolder;
import com.androidvizlab.bachelor.customcomponents.viewholder.ListViewHolder;
import com.androidvizlab.bachelor.datamodels.Camera;
import com.androidvizlab.bachelor.datamodels.CameraGroup;
import com.androidvizlab.bachelor.datamodels.UniqueCamGroups;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

/**
 * A subclass of ListAdapter use to manage the ListView.
 * This class is use to present "triplets of camera group" and the camera information
 * within each group in a list(ListView).
 * 
 * Used for displaying list camera group or camera
 * @author The Hive
 */
public class CustomListAdapter extends ArrayAdapter<Object>
{
	private int layoutID = 0; //the id of the xml 
	
	private LayoutInflater inflater = null; //used to inflate Views and other UI elements
	
	private ListViewHolder holder = null; //View holder used for recycling Views in the list
	
	public CustomListAdapter(Context context, int viewResourceId, ArrayList<Object> objects) {
		super(context, viewResourceId,objects);
		
		this.layoutID = viewResourceId;
		
		//Instantiate an inflater from context
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{	
		switch(layoutID)
		{
		case R.layout.caminfo: //inflate the xml for displaying information about the camera within each a selected group
			
			if(convertView == null)
			{
				convertView = inflater.inflate(layoutID,parent,false);
				holder = new CamInfoListViewHolder(convertView);
				convertView.setTag(holder);
			}
			else
			{
				holder = (CamInfoListViewHolder) convertView.getTag();
			}
			
			Camera ci = (Camera) getItem(position);
			
			((CamInfoListViewHolder)holder).setTVText(Integer.toString(ci.getCamNo()),
					Double.toString(ci.getC()),Double.toString(ci.getXh()),
					Double.toString(ci.getYh()),Double.toString(ci.getOrt()),
					Float.toString(ci.getF1()),Float.toString(ci.getF2()),
					Float.toString(ci.getP1()),Float.toString(ci.getP2()));
			
			break;
		
		case R.layout.sortedcamcombo_list: //displays the groups of triplet camera groups.
			
			if(convertView == null)
			{
				convertView = inflater.inflate(layoutID,parent,false);
				holder = new CamComboListViewHolder(convertView);
				convertView.setTag(holder);
				
				UniqueCamGroups uniqueCamGrp = (UniqueCamGroups) getItem(position);
				
				for(CameraGroup grp : uniqueCamGrp.getGroups())
				{
					Log.i("datates",grp.getGroupName());
				}
				
				((CamComboListViewHolder)holder).addRowElements(uniqueCamGrp);
			}
		
			/*
			if(convertView == null)
			{
				convertView = inflater.inflate(layoutID,parent,false);
				holder = new CamComboListViewHolder(convertView);
				convertView.setTag(holder);
			}
			else
			{
				holder = (CamComboListViewHolder) convertView.getTag();
			}
			
			CameraGroup cc = (CameraGroup) getItem(position);
			
			String name = "";
			
			for(int i = 0; i < cc.getCameraList().size(); i++)
			{
				name += cc.getCameraList().get(i).getCamNo()+" ";
			}
			
			((CamComboListViewHolder)holder).setTVText(name,
					Double.toString(cc.getS0()),Double.toString(cc.getMean()),
					Double.toString(cc.getMax()),Integer.toString(cc.getNoOfFramesUsed()));
			*/
			break;
		}
				
		return convertView;
	}
	
	/**
	 * Adds new list of contents. This function should be followed with a call
	 * to notifyDataSetChanged() to update the view.
	 * 
	 * @param newList updated list content
	 */
	public void addAll(ArrayList<Object> newList)
	{
		this.addAll(newList); //method in the ArrayAdapter<T> class.
		Log.i("viz_result_data","data received:adapter");
	}
}
